package org.jeecg.modules.flow.callback.outbound.notice;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouseArea;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseAreaService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.third.service.IMesStatusService;
import org.jeecg.modules.flow.callback.outbound.util.CommonOutboundUtil;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 出库任务通知Mes库区 回调bean
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-09-26
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class OutBoundTaskNoticeMesPositionBean<T> extends BaseCustomBeanRun<T> {

    private final CommonOutboundUtil commonOutboundUtil;
    private final IMesStatusService mesStatusService;
    private final IWmsOutReceiptTaskService wmsOutReceiptTaskService;
    private final IWmsOutReceiptTaskMaterialService wmsOutReceiptTaskMaterialService;
    private final IWmsBasicWarehouseAreaService wmsBasicWarehouseAreaService;

    /**
     * 业务逻辑 出库任务提交后通知mes
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String taskId = baseFlowDto.getInfoId();
            log.info("开始通知mes库区库位");
            List<WmsOutReceiptTaskMaterial> wmsOutReceiptTaskMaterialList = wmsOutReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>()
                    .eq(WmsOutReceiptTaskMaterial::getWmsOutReceiptTaskId, taskId)
            );
            if (!CollectionUtil.isEmpty(wmsOutReceiptTaskMaterialList)) {
                try {
                    List<JSONObject> collect = wmsOutReceiptTaskMaterialList.stream().map(o -> {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("batchId", o.getBatchCode());
                        log.info("库区id{}",o.getWmsBasicWarehouseAreaId());
                        if (!StrUtil.isEmpty(o.getWmsBasicWarehouseAreaId())) {
                            WmsBasicWarehouseArea wmsBasicWarehouseArea = wmsBasicWarehouseAreaService.getById(o.getWmsBasicWarehouseAreaId());
                            log.info("库区信息{}",wmsBasicWarehouseArea);
                            if (!Objects.isNull(wmsBasicWarehouseArea)) {
                                jsonObject.put("position", wmsBasicWarehouseArea.getCode());
                            }else {
                                jsonObject.put("position", "A");
                            }
//                            else{
//                                throw new JeecgBootException("物料库区信息不存在，无法通知mes库区信息");
//                            }
                        }else {
                            jsonObject.put("position", "A");
                        }
//                        else{
//                            throw new JeecgBootException("物料库区信息不存在，无法通知mes库区信息");
//                        }
                        return jsonObject;
                    }).collect(Collectors.toList());
                    // 通知MES库区库位
                    mesStatusService.outbound(taskId, null, 3, collect);
                } catch (Exception e) {
                    log.error("通知失败{}", e.getMessage());
                }
        }
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(taskId);
    }
}
